package com.minsu.aop;

import com.minsu.entity.ProductionOrder;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Date;

@Aspect
@Component
public class InTimeAspect {

    // 定义切入点，这里假设切入点是 ProductionOrdersMapper 的 add 和 update 方法
    @Pointcut("execution(* com.minsu.mapper.ProductionOrdersMapper.add(..)) || execution(* com.minsu.mapper.ProductionOrdersMapper.update(..))")
    public void productionOrderMethods() {}

    @Around("productionOrderMethods()")
    public Object aroundProductionOrderMethods(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            if (arg instanceof ProductionOrder) {
                ProductionOrder productionOrder = (ProductionOrder) arg;
                if (productionOrder.getInTime() == null) {
                    productionOrder.setInTime(new Date());
                }
            }
        }
        return joinPoint.proceed(args);
    }
}
